﻿/// <reference name="../../lib/jquery-1.3.1-vsdoc.js" />
/*!
*   My dialog(Jquery Plugin)
*
* Copyright (c) 2009 Andy Huang
* Date: Monday, February 09, 2009
* Version: V1.0
*/
(function($) {
    // plugin dialog
    $.fn.dialog = function(options, fn) {

        // build main options before element iteration
        var opts = $.extend({}, $.fn.dialog.defaults, options);

        // iterate and reformat each matched element
        return this.each(function() {
            $this = $(this);
            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

            $.fn.dialog.defaults.template = o.template;
            $.fn.dialog.defaults.title = o.title;
            $.fn.dialog.defaults.width = o.width;
            $.fn.dialog.defaults.left = o.left;
            $.fn.dialog.defaults.firstboxval = o.firstboxval;
            $.fn.dialog.defaults.secondboxval = o.secondboxval;
            $.fn.dialog.defaults.multipleboxval = o.multipleboxval;

            $this.empty().show();
            body();
            $("#dialog-main").css({ width: o.width, left: o.left });

            switch (o.mode) {
                case 'sample':
                    $(".dialog-content").append(namebox()).append("<br />").append(sumbitspan());
                    break;
                case 'two-edit':
                    $(".dialog-content").append(namebox()).append("<br />").append(newNamebox()).append("<br />").append(sumbitspan());
                    break;
                case 'two-new':
                    $(".dialog-content").append(namebox()).append("<br />").append(multiplebox()).append("<br />").append(sumbitspan());
                    break;
            }

            $("#dialog-bg").show().animate({ opacity: "0.5" }, "normal");
            $("#dialog-main").animate({ top: ($(document).scrollTop() + 50) + "px" }, "normal");

            close();
            submit(fn);
        });

    };

    // plugin defaults
    $.fn.dialog.defaults = {
        mode: 'text',
        title: 'New',
        width: 530,
        left: '20%',
        firstboxval: '',
        secondboxval: '',
        multipleboxval: '',
        template: ''
    };

    $.fn.dialog.close = function() {
            $("#dialog-bg").animate({ opacity: "0" }, "normal", function() { $(this).hide(); });
            $("#dialog-main").animate({ top: ($(document).scrollTop() - $this.height()) + "px" }, "normal", function() { $(this).hide(); });
    };

    function submit(fn) {
        $("#dialog-submit").click(function() {
            var info = { firstval: $("#jquery-box-text").val(), secondval: $("#jquery-box-text-new").val(), multipleval: $("#jquery-box-text-multiple").val() };
            fn(info);
        });

    }

    function body() {
        var dopt = $.fn.dialog.defaults;
        var b = "<div id=\"dialog-bg\" style=\"height:" + $(document).height() + "px;filter:alpha(opacity=0);opacity:0;\"></div>";
        b += "<div id=\"dialog-main\" class=\"dialog-main\">";
        b += "<div class=\"dialog-title\"><h5>" + dopt.title + "</h5><span id='dialog-close'>[Close]</span></div>";
        b += "<div class=\"dialog-content\">" + dopt.template + "</div>";
        b += "</div>";
        $this.append(b);
    }

    function close() {
        $("#dialog-close").click(function() {
            $this.hide();
        });

        $("#dialog-close-button").click(function() {
            $.fn.dialog.close();
        });
    }

    function namebox() {
        var str = "Name:<br/><input type='text' id='jquery-box-text' value='" + $.fn.dialog.defaults.firstboxval + "' style='width:500px;' />";
        return str;
    }

    function newNamebox() {
        var str = "New Name:<br/><input type='text' id='jquery-box-text-new' value='" + $.fn.dialog.defaults.secondboxval + "' style='width:500px;' />";
        return str;
    }

    function multiplebox() {
        var str = "Text:<br/><textarea id='jquery-box-text-multiple' style='width:500px; height:300px' >" + $.fn.dialog.defaults.multipleboxval + "</textarea>";
        return str;
    }

    function sumbitspan() {
        var str = "<center><div style='padding:2px;'> ";
        str += "<input type='button' id='dialog-submit' value='Submit' />&nbsp;<input type='button' value='Close' id='dialog-close-button' /> ";
        str += "</div></center>";
        return str;
    }

})(jQuery);